import { defineStore } from "pinia";
import { message } from "ant-design-vue";
import { defaultMenus, tagMap } from "@/utils";

export const userStore = defineStore(`${import.meta.env.VITE_APP_TERRACE}-store`, {
  state: () => ({
    isRouterAlive: true,
    currentConf: {
      menu1_active: 0,
      menu2_active: 0,
      menu3_active: 0
    },
    currentComName: "device_0",
    currentAreaName: "设备分区图",
    childArea: defaultMenus[0].children[0].children || [],
    modelTab: {
      name: "",
      comName: ""
    },
    operateList: [
      // {
      //   index: 0,
      //   currentComName: "",
      // }
    ], //用户操作保存的数据  存在close 不存在open

    wsOperateList: [
      //ws触发的
      // {
      //   index: 0,
      //   currentComName: "",
      //   value:0
      // }
    ]
  }),
  getters: {
    getCurrentConf: (state) => {
      return state.currentConf;
    },
    getCurrentComName: (state) => {
      return state.currentComName;
    },
    getChildArea: (state) => {
      return state.childArea;
    },
    getCurrentAreaName: (state) => {
      return state.currentAreaName;
    },
    getmodelTab(state) {
      return tagMap[state.getCurrentComName] || {};
    },
    getOperateList(state) {
      return state.operateList.concat(state.wsOperateList);
    },
    getWsOperateList(state) {
      return state.wsOperateList;
    }
  },
  actions: {
    chanegCurrentConf(data) {
      this.currentConf = { ...this.currentConf, ...data };
    },
    chanegCurrentComName(data) {
      this.currentComName = data;
    },
    chanegChildArea(data) {
      this.childArea = data;
    },
    chanegCurrentAreaName(data) {
      this.currentAreaName = data;
    },
    changeOperateList(obj) {
      if (Object.prototype.toString.call(obj) === "[object Array]") {
        //清空操作
        this.operateList = [];
        return;
      }
      if (!this.operateList.length) {
        this.operateList = [obj];
      }
      const index = this.operateList.findIndex((v) => v.currentComName === obj.currentComName && v.index === obj.index);
      if (index > -1) {
        this.operateList[index] = obj;
      } else {
        this.operateList.push(obj);
      }
    },
    changeModelOperateList(arr) {
      if (!arr.length) {
        return;
      }
      arr.forEach((obj) => {
        const index = this.operateList.findIndex(
          (v) => v.currentComName === obj.currentComName && v.index === obj.index
        );

        if (obj.value === 0) {
          //恢复
          if (index > -1) {
            this.operateList.splice(index, 1);
          } else {
            this.operateList.push(obj);
          }
        } else {
          //关闭
          if (index > -1) {
            this.operateList[index] = obj;
          } else {
            this.operateList.push(obj);
          }
        }
      });
    },
    changeWsOperateList(arr) {
      if (!this.wsOperateList.length) {
        this.wsOperateList = arr;
      }

      arr.forEach((obj) => {
        const index = this.wsOperateList.findIndex(
          (v) => v.currentComName === obj.currentComName && v.index === obj.index
        );
        if (index > -1) {
          this.wsOperateList[index] = obj;
        } else {
          this.wsOperateList.push(obj);
        }
      });
    },
    changeOperateListRemove(obj) {
      const index = this.operateList.findIndex((v) => v.currentComName === obj.currentComName && v.index === obj.index);
      this.operateList.splice(index, 1);
    },
    login(data) {
      return new Promise((resolve, reject) => {
        if (data.account !== "admin") {
          reject(false);
          return message.error("用户名输入错误！");
        } else if (data.password !== "admin") {
          reject(false);
          return message.error("密码输入错误！");
        } else {
          resolve(true);
          return message.success("欢迎登录！");
        }
      });
    },
    logout() {
      return new Promise((resolve) => {
        this.currentConf = {
          menu1_active: 0,
          menu2_active: 0,
          menu3_active: 0
        };
        this.currentComName = "device_0";
        this.currentAreaName = "设备分区图";
        this.childArea = defaultMenus[0].children[0].children || [];
        this.modelTab = {
          name: "",
          comName: ""
        };
        this.operateList = [];
        resolve(true);
      });
    },
    pageRedict() {
      return new Promise((resolve) => {
        this.currentConf = {
          menu1_active: 0,
          menu2_active: 0,
          menu3_active: 0
        };
        this.currentComName = "";
        this.currentAreaName = "";
        this.childArea = [];
        this.modelTab = {
          name: "",
          comName: ""
        };
        this.operateList = [];
        resolve(true);
      });
    }
  },
  persist: {
    enabled: true,
    strategies: [
      {
        storage: sessionStorage,
        paths: [
          "currentConf",
          "currentComName",
          "childArea",
          "currentAreaName",
          "modelTab",
          "operateList",
          "wsOperateList"
        ]
      }
    ]
  }
});
